package com.tensai.study.新概念.响应式编程.reactor.flux;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.util.function.Tuple2;

import java.util.concurrent.atomic.AtomicInteger;

public class ZipTest {

    public void test() {

    }

    /**
     * zipWith 将多个元素合并成元组，并从每个source产生元素直至结束
     */
    @Test
    public void zipWithTest() {
        final AtomicInteger index = new AtomicInteger();
        Flux<Tuple2<String, String>> tuple2Flux = Flux.just("a", "b")
                .zipWith(Flux.just("c", "d"));
//subscribe1
        tuple2Flux.subscribe(System.out::println);
        System.out.println("————— 分割线1 —————");

//subscribe2
        tuple2Flux.subscribe(tupleFlux -> {
            System.out.println("t1—————" + index + "—————>" + tupleFlux.getT1());
            System.out.println("t2—————" + index + "—————>" + tupleFlux.getT2());
            index.incrementAndGet();
        });
        System.out.println("————— 分割线2 —————");

    }

    @Test
    public void zipWithTest2() {
        Flux.just("a", "b")
                .zipWith(Flux.just("c", "d"), (s1, s2) -> String.format("%s-%s", s1, s2))
                .subscribe(System.out::println);

    }
}
